#pragma once

#include <vector>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include <strings.h>
#include <sys/epoll.h>

#include "Logger.h"
#include "Poller.h"
#include "Channel.h"
#include "Timestamp.h"

/**
 * 封装了epoll的使用：epoll_create epoll_ctl epoll_wait
*/ 

class EPollPoller : public Poller
{
public:
    EPollPoller(EventLoop* loop);
    ~EPollPoller() override;

    // 重写基类Poller的抽象方法
    Timestamp poll(int timeoutMs, ChannelList* activeChannels) override;
    // 更新Channel所感兴趣的事件
    void updateChannel(Channel* channel) override;
    // 将Channel从EventLoop中移除
    void removeChannel(Channel* channel) override;

private:
    static const int kInitEventListSize = 16;

    // 填写活跃的连接
    void fillActiveChannels(int numEvents, ChannelList* activeChannels) const;
    // 更新Channel通道
    void update(int operation, Channel* channel);

    using EventList = std::vector<struct epoll_event>;

    int epollfd_; // epoll句柄
    EventList events_;
};